package cn.edu.scut.sys.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.edu.scut.sys.dao.TrainScoreItemDao;
import cn.edu.scut.sys.service.TrainScoreItemService;
import cn.edu.scut.sys.utils.DataRequest;
import cn.edu.scut.sys.utils.DataResponse;
import cn.edu.scut.sys.vo.TrainScoreItem;

@Component("trainScoreItemService")
public class TrainScoreItemServiceImpl implements TrainScoreItemService{

private TrainScoreItemDao trainScoreItemDao;
	
	@Resource(name="trainScoreItemDao")
	public void setUserDao(TrainScoreItemDao trainScoreItemDao) {
		this.trainScoreItemDao = trainScoreItemDao;
	}

	@Override
	public DataResponse<TrainScoreItem> list(DataRequest request,String trainScoreIdStr) {
		DataResponse<TrainScoreItem> response = new DataResponse<TrainScoreItem>();  
        int count;//总记录数  
        int limit = request.getRows() <= 0 ? 20 : request.getRows();//每页显示数量  
        int totalPages;//总页数  
        int page = request.getPage() <= 0 ? 1 : request.getPage();//当前显示页码  
        
        List<TrainScoreItem> list;  
          
        count = trainScoreItemDao.count().intValue();  
        totalPages = count / limit;  
        if (count % limit != 0) {  
            totalPages++;  
        }  
        int currPage = Math.min(totalPages, page);  
          
        int start = currPage * limit - limit;  
        start = start < 0 ? 0 : start;  
        Long trainScoreId = 0L;
        try{
        	
        	trainScoreId = Long.parseLong(trainScoreIdStr);
        } catch(Exception ex) {
        	trainScoreId = 0L;
        }  
        
        
        
        list = trainScoreItemDao.list(start, limit, request.getSord(), request.getSidx(),trainScoreId);  
        response.setRecords(count);  
        response.setTotal(totalPages);  
        response.setPage(currPage);  
        response.setRows(list);  
          
        return response;  
	}

	@Override
	public boolean add(TrainScoreItem trainScoreItem) {
		trainScoreItemDao.insert(trainScoreItem);
		return true;
	}

	@Override
	public TrainScoreItem get(Long id) {
		return trainScoreItemDao.get(id);
	}

	@Override
	public boolean update(TrainScoreItem trainScoreItem) {
		trainScoreItemDao.update(trainScoreItem);
		return true;
	}
}
